// 保存文件
import { ElNotification } from "element-plus";

//保存excel文件
export function saveXlsx(fileData: any, fileName: string) {
  const fileType =
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8";

  const blob = new Blob([fileData], { type: fileType });
  const downloadUrl = window.URL.createObjectURL(blob);

  const downloadLink = document.createElement("a");
  downloadLink.href = downloadUrl;
  downloadLink.download = fileName;

  document.body.appendChild(downloadLink);
  downloadLink.click();

  document.body.removeChild(downloadLink);
  window.URL.revokeObjectURL(downloadUrl);
  ElNotification({
    title: "下载通知",
    message: `已成功下载文件：${fileName}`,
    type: "success",
  });
}

//处理导出结果
export function handleExportResponse(res: any) {
  const fileData = res.data;
  const fileName = decodeURI(
    res.headers["content-disposition"].split(";")[1].split("=")[1]
  );
  // 下载文件
  saveXlsx(fileData, fileName);
}
